package com.example.myapplication8;

import com.example.myapplication8.EmptyCollectionException;
import com.example.myapplication8.ElementNotFoundException;

public class Product<T>{

    public  int count;
    public  Productset head;
    public Productset tail;

//    public static void main(String[] args) {
//        Product prduct = new Product();
//
//        prduct.addToFront("方便面", 10, 3.5);
//        prduct.addToFront("苹果", 10, 5);
//        prduct.addToFront("西瓜",10,15);
//        prduct.addToFront("盆",20,50);
//        prduct.addToFront("橘子",10,10);
//        prduct.addAfter("香肠",10,11,"苹果");
//        System.out.println(prduct.toString());
//        prduct.remove("西瓜");
//        System.out.println(prduct.toString());
//        prduct.selectsort();
//        System.out.println(prduct.toString());
//    }


    public Product() {
        count = 0;
        head = tail= null;
    }

    public void addAfter(T sum, T price, T name, String target) {
        Productset list = new Productset(sum, price, name);
        Productset current = null;
        Productset current1 = head;

        while (current1.getNext() != null && !current1.getName().toString().equals(target)) {
            current1 = current1.getNext();
            current = current1.getNext();
        }
        if (current1.getNext() == null) {
            tail.setNext(list);
            tail = list;
            count++;
        } else {
            current1.setNext(list);
            list.setNext(current);
            count++;
        }
    }

    public void addToRear(T sum, T price, T name) {
        Productset list = new Productset(sum, price, name);
        if (head==null) {
            head =list;
            tail = list;
            count++;
        } else {
            tail.setNext(list);
            tail = list;
            count++;
        }
        }

    public void addToFront(T sum, T price, T name) {
        Productset list1 = new Productset(sum, price, name);
        if (head == null) {
            head =list1;
            tail=list1;
            } else {
            Productset current1=head;
            while(current1.getNext()!=null) {
                current1 = current1.getNext();
            }
            list1.setNext(head);
            head = list1;
            tail=current1;
            }
        count++;
    }

    public void selectsort() {
        Productset number=head;
        while (number!=null) {
          Productset list=number.getNext();
            while (list!= null) {
                 T a= (T) list.getSum();
                T b= (T) list.getPrice();
                T c= (T) list.getName();
                if (list.compareTo(number)>0) {
                    list.setName(number.getName());
                    list.setPrice(number.getPrice());
                    list.setSum(number.getSum());
                    number.setName(c);
                    number.setPrice(b);
                    number.setSum(a);
                }
                list = list.getNext();
            }
            number = number.getNext();
        }
    }

    public String find(String name) throws EmptyCollectionException {

        Productset list = head;
        while (list.getNext()!=null&&!list.getName().toString().equals(name)) {
            list = list.getNext();
        }

        if (!list.getName().toString().equals(name)) {
            return "没找到您查找的商品,请核对商品名称";
        } else {
            return list.toString();
        }
    }

    public boolean isEmpty() {
        return count == 0;
    }
    @Override
    public String toString() {
        Productset list = head;
        String result = "";

        while (list.getNext() != null) {
            result += "名称" + ":" + list.getName() + " " + "价格" + ":" + list.getPrice() + " " + "总量" + ":" + list.getSum() + "\n";
            list = list.getNext();
        }
        result += "名称" + ":" + list.getName() + " " + "价格" + ":" + list.getPrice() + " " + "总量" + ":" + list.getSum() + "\n";
        return result;
    }

    public String remove(String targetElement) throws EmptyCollectionException,
            ElementNotFoundException {
        if (isEmpty()) {
            throw new EmptyCollectionException("LinkedList");
        }

        boolean found = false;
        Productset previous = null;
        Productset current = head;

        while (current != null && !found) {
            if (targetElement.equals((String) current.getName())) {
                found = true;
            } else {
                previous = current;
                current = current.getNext();
            }
        }

        if (!found) {
            throw new ElementNotFoundException("LinkedList");
        }

        if (count == 1)  // 列表中只有一个元素
        {
            head = tail = null;
        } else if (current.equals(head))  // 目标在头部
        {
            head = current.getNext();
        } else if (current.equals(tail))  // 目标在尾部
        {
            tail = previous;
            tail.setNext(null);
        } else  // 目标在中间
            previous.setNext(current.getNext());

        count--;

        return current.toString();
    }
    class Productset<T> implements Comparable<Productset> {
        Productset next ;
        private T sum;
        private T price;
        private T name;
        public Productset(T sum, T price, T name) {
            next = null;
            this.sum = sum;
            this.price = price;
            this.name = name;
            }

        public Productset getNext() {
            return next;
        }

        public void setNext(Productset node) {
            next = node;
        }

        public T getSum() {
            return sum;
        }

        public T getName() {
            return name;
        }

        public T getPrice() {
            return price;
        }

        public void setSum(T sum1) {
            sum = sum1;
        }

        public void setPrice(T price1) {
            price = price1;
        }

        public void setName(T name1) {
            name = name1;
        }
        @Override
        public String toString(){
            return "名称" + ":" + getName() + " " + "价格" + ":" + getPrice() + " " + "总量" + ":" + getSum()+"\n";
        }


        @Override
        public int compareTo( Productset o) {
              Comparable<T> comparableElement = (Comparable<T>) getName();
            if(comparableElement.compareTo((T) o.getName())>0)
                return 1;
            else
                return -1;

        }
    }


}